﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using FundRaising.BLL;
using FundRaising.GUI;

namespace HumanResorcesProj.GUI.Extensions
{



/// <summary>
/// Reponsible for flushing out the file from the database to the user.
/// </summary>
public class FileStream : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {
	// get the file id
      
        //Guid fileId = new Guid(context.Request.QueryString["FileId"]); ORGINAL

        Guid fileId = new Guid(EditRequest.Curr.TheSelectedFile.ToString());
     // Guid fileId = new Guid(SearchEmplyee.Curr.TheSelectedFile.ToString());
      // shady there is AN ERR
	using (SqlCommand command = new SqlCommand())
	{
		// get the file from database
        command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Eden-DatabaseConnectionString3"].ConnectionString);
		command.CommandText = "select * from [RequestFile] where FileId = @FileId";
		command.Parameters.Add("@FileId", SqlDbType.UniqueIdentifier).Value = fileId;
		command.Connection.Open();
		SqlDataReader reader = command.ExecuteReader();
		if (reader.Read())
		{
			// flush out the binary data to the user
			context.Response.Clear();
			context.Response.ContentType = (string) reader["FileType"];
			context.Response.AddHeader("Content-Disposition", String.Format("inline;filename={0};", reader["FileName"].ToString()));
			context.Response.AddHeader("Content-Length", reader["FileSize"].ToString());
			context.Response.BinaryWrite((byte[]) reader["FileContent"]);
			context.Response.End();
		}
	}
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

}
